package com.example.shishaolong.warehousemanager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.shishaolong.warehousemanager.entity.GoodsEntity;
import com.example.shishaolong.warehousemanager.entity.GoodsViewHolder;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {

    private List<GoodsEntity> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public GoodsAdapter(Context context, List<GoodsEntity> list){
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        /*GoodsViewHolder holder = null;
        if(convertView == null){
            holder = new GoodsViewHolder();
            //获取到了单个货物界面
            convertView = mInflater.inflate(R.layout.goods_item,null);
            //界面元素绑定
            holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox1);
            holder.mCurNum = (TextView)convertView.findViewById(R.id.curNum_textView);
            holder.mDate = (TextView)convertView.findViewById(R.id.date_textView);
            holder.mName = (TextView) convertView.findViewById(R.id.name_textView);
            holder.mPerson = (TextView) convertView.findViewById(R.id.person_textView);
            holder.mTotalNum = (TextView) convertView.findViewById(R.id.totalNum_textView);
        }else{
            holder = (GoodsViewHolder)convertView.getTag();
        }
//        holder.mCheckBox.setChecked(mList.get(position).isChecked());
        holder.mCurNum.setText(mList.get(position).getCurrentNum()+"");
        holder.mDate.setText(mList.get(position).getBuyDate());
        holder.mName.setText(mList.get(position).getName());
        holder.mPerson.setText(mList.get(position).getBuyerName());
        holder.mTotalNum.setText(mList.get(position).getAllNum()+"");
        return convertView;
*/

        //ArrayList 中取出一个GoodsEntity
        GoodsEntity goods = (GoodsEntity) getItem(position);

        //获取显示数据对象的布局文件
        View view;
        if(convertView == null){
            view =  mInflater.inflate(R.layout.goods_item,null);
        }else {
            view = convertView;
        }

        //获取布局上的View对象，
        CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox1);
        TextView name = (TextView) view.findViewById(R.id.name_textView);
//        TextView totalNum = (TextView)view.findViewById(R.id.totalNum_textView);
//        TextView curNum = (TextView)view.findViewById(R.id.curNum_textView);
        TextView person = (TextView)view.findViewById(R.id.person_textView);
        TextView date = (TextView)view.findViewById(R.id.date_textView);

        //将实体类中的数据填写到各自的TextView中
        checkBox.setChecked(goods.isChecked());
        name.setText(goods.getName());
//        totalNum.setText(goods.getAllNum()+"");
//        curNum.setText(goods.getCurrentNum()+"");
        person.setText(goods.getBuyerName());
        date.setText(goods.getStorageDate());

        return view;
    }
}
